<template>
  <div>
    <template>
      <div class="articleList" ref="listBox" @scroll="onScroll">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <!-- 
        load对应的onLoad函数:
            这是专门用来加载数据的函数
        这个函数会在两种情况下自动调用
            1. 数据没有铺满一屏时会调用
            2. 当你向上拉需要加载出更多数据时调用

        finished用来标记数据是否全部加载完毕
            当它为true,就代表加载完了,所以不会再调用onLoad
            当它为false时,就代表没加载完,onLoad还可以遵照上面的原则自动调用

        v-model绑定loading是用来标记list是否正在加载中
            为false,代表当前没有在加载,可以进行下次加载
            为true,代表当前正在加载中,为了效率问题,此时不会调用onLoad
            当它为false时,onLoad才能被调用,并且onLoad调用时会自动把loading改成true
                因为onLoad被调用就代表要去加载数据了,所以会自动把loading改成true,代表当前正在加载
     -->
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell v-for="(item, index) in list" :key="index">
              <div
                @click="
                  $router.push({
                    name: 'particulars',
                    query: {
                      id: item.art_id,
                    },
                  })
                "
              >
                {{ item.title }}
              </div>
              <van-grid :border="false" :column-num="item.cover.type">
                <van-grid-item
                  v-for="(pic, idx) in item.cover.images"
                  :key="idx"
                >
                  <van-image :src="pic" />
                </van-grid-item>
                <div>
                  <span>{{ item.aut_name }}</span>
                  <span>({{ item.comm_count }})评论</span>
                  <span>{{ item.pubdate | relvTime }}</span>
                  <van-icon name="cross" />
                </div>
              </van-grid>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </div>
    </template>
  </div>
</template>

<script>
import { articleListAPI } from "@/api";
export default {
  props: ["channel_id"],
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      isLoading: false,
      scTop: 0,
    };
  },
  activated() {
    this.$refs.listBox.scrollTop = this.scTop;
  },
  methods: {
    onScroll() {
      // 拿到滚动的距离，保存起来
      this.scTop = this.$refs.listBox.scrollTop;
    },
    async onLoad() {
      let res = await articleListAPI({
        channel_id: this.channel_id,
        timestamp: this.timestamp,
      });
      // console.log(res);
      this.list.push(...res.data.data.results);
      this.timestamp = res.data.data.pre_timestamp;
      this.loading = false;

      if (res.data.data.pre_timestamp == null) {
        this.finished = true;
      }
    },

    async onRefresh() {
      let res = await articleListAPI({
        channel_id: this.channel_id,
        timestamp: Date.now(),
      });
      this.list = res.data.data.results;
      this.isLoading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.articleList {
  position: fixed;
  width: 100vw;
  top: 90px;
  bottom: 50px;
  overflow: auto;
}
::v-deep .van-grid {
  line-height: 30px;
  position: relative;
  span {
    font-size: 10px;
    color: #ddd;
    margin-right: 10px;
  }
  .van-icon-cross {
    position: absolute;
    right: 0px;
    bottom: 10px;
    font-size: 10px;
    color: #ddd;
  }
}
</style>
